package beautiful.butterfly.drds.binlog.parser;


import beautiful.butterfly.drds.binlog.connection.ConnectionAttributes;
import beautiful.butterfly.drds.binlog.event.EventData;
import beautiful.butterfly.drds.binlog.event.data.RotateEventData;
import beautiful.butterfly.drds.binlog.netty.server.NioSocketChannelExtend;
import beautiful.butterfly.drds.binlog.utils.ByteUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;

public class RotateEventDataParser implements EventDataParser
{


    @Override
    public EventData parse(ByteBuf msg, ChannelHandlerContext context, int checksumLength)
    {
        // TODO Auto-generated method stub
        // LoggerUtils.debug(logger, "available:" + msg.readableBytes());
        long binlogPosition = ByteUtils.readUnsignedLong(msg, 8);
        String binlogFileName = ByteUtils.readSpecifiedLengthString(msg,
                ByteUtils.availableWithChecksumLength(msg, checksumLength));
        // 第一步及时保留到全局对象,供后续处理
        ConnectionAttributes connectionAttributes = ((NioSocketChannelExtend) context.channel()).getConnectionAttributes();
        // Logger
        RotateEventData rotateEventData = new RotateEventData();
        rotateEventData.setBinlogFilename(binlogFileName);
        rotateEventData.setBinlogFilePosition(binlogPosition);
        return rotateEventData;
    }

}
